package wells.painio;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;

public class PlainOioServer {
	public static void main(String[] args) {
		PlainOioServer pos = new PlainOioServer();

		try {
			pos.serve(9999);
		} catch (IOException e) {
			 e.printStackTrace();
		}
	}

	public void serve(int port) throws IOException {
		final ServerSocket socket = new ServerSocket(port); // 1
		try {
			for (;;) {
				final Socket clientSocket = socket.accept(); // 2
				System.out.println("Accepted connection from " + clientSocket);

				new Thread(() -> {
					OutputStream out;
					try {
						out = clientSocket.getOutputStream();
						out.write("Hi!\r\n".getBytes(Charset.forName("UTF-8"))); // 4
						out.flush();
						clientSocket.close(); // 5

					} catch (IOException e) {
						e.printStackTrace();
						try {
							clientSocket.close();
						} catch (IOException ex) {
							// ignore on close
						}
					}
				}).start(); // 6
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
